package com.nsgf.library.ui.page.manager;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.data.bean.XBannerBeanInfo;
import com.nsgf.library.databinding.DialogNsgfAbnormalBinding;
import com.nsgf.library.glide.GlideImgManager;
import com.nsgf.library.photopicker.PhotoPreview;
import com.nsgf.library.photopicker.widget.MultiPickResultView;
import com.nsgf.library.ui.adapter.AbnormalListAdapter;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.fragment.MessageFragment;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;

public class QueryAbnormalFragment extends BaseFragment {

    public static QueryAbnormalFragment newInstance(List<WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal> abnormal) {
        QueryAbnormalFragment messageFragment = new QueryAbnormalFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("abnormal", (ArrayList<? extends Parcelable>) abnormal);
        messageFragment.setArguments(bundle);
        return messageFragment;

    }

    private List<WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal> getAbnormal() {
        return getArguments().getParcelableArrayList("abnormal");
    }

    private String getType() {
        return getArguments().getString("type");
    }

    private MessageFragment.MessageFragmentStates mStates;

    private AbnormalListAdapter mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(MessageFragment.MessageFragmentStates.class);

    }


    @Override
    protected DataBindingConfig getDataBindingConfig() {

        mAdapter = new AbnormalListAdapter(getContext());

        return new DataBindingConfig(R.layout.fragment_nsgf_abnormal, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mAdapter.submitList(getAbnormal());
        for (WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal abnormal : mAdapter.getCurrentList()) {
            abnormal.setSelectAbnormal(abnormal.getAbnormal());
        }
        mAdapter.notifyDataSetChanged();


        mAdapter.setOnItemClickListener((viewId, item, position) -> {

            List<XBannerBeanInfo> banners = new ArrayList<>();

            dialog = new Dialog(getActivity(), R.style.dialogstyle);
            View view12 = View.inflate(getActivity(), R.layout.dialog_nsgf_abnormal, null);
            DialogNsgfAbnormalBinding binding = DataBindingUtil.bind(view12);
            binding.setVm(item);

            if (item.getLiveImageList().size() == 0) {
                banners.add(new XBannerBeanInfo(""));
            } else {
                for (WorkOrderDetailBean.FileDto abnormal : item.getLiveImageList()) {
                    banners.add(new XBannerBeanInfo(APIs.BASE_URL_IMG + abnormal.getFilePath()));
                }
            }

            binding.ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            binding.xbanner.setBannerData(banners);
            binding.xbanner.loadImage((banner, model, view1, position1) -> {
                String url = (String) ((XBannerBeanInfo) model).getXBannerUrl();
                GlideImgManager.getInstance().glideLoaderBannerCenterCrop(getActivity(), url, (ImageView) view1, 10);

            });


            binding.xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                @Override
                public void onItemClick(XBanner banner, Object model, View view, int position) {
                    ArrayList<WorkOrderDetailBean.FileDto> fileDtos = new ArrayList<>();
                    WorkOrderDetailBean.FileDto fileDto = new WorkOrderDetailBean.FileDto();
                    fileDto.setFilePath(banners.get(position).getXBannerUrl().toString());
                    fileDtos.add(fileDto);
                    PhotoPreview.builder()
                            .setPhotos(fileDtos)
                            .setAction(MultiPickResultView.ACTION_ONLY_SHOW)
                            .start(getActivity());
                }
            });

            binding.radioGroup.setVisibility(View.GONE);
            binding.tvNsgfAbnormalStatus.setText(item.getAbnormal() ? "是" : "否");

            dialog.setContentView(view12);
            Window window = dialog.getWindow();
            window.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = getResources().getDisplayMetrics().widthPixels;
            window.setAttributes(lp);
            window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
            dialog.show();
        });

    }

    public List<WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal> getAbnormalList() {
        return mAdapter.getCurrentList();
    }

    private Dialog dialog;

}
